package com.gitee.simons.common.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cglib.beans.BeanCopier;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import net.sf.cglib.beans.BeanMap;


public class BeanTransfer {
	
	private BeanTransfer(){
		//私有构造
	}

	private static final Map<String,Map<String, BeanCopier>> BEAN_COPIER_MAP = new HashMap<String,Map<String, BeanCopier>>();
	
	public static <Source, Target> Target transferColumn(Source source, Class<Source> sourceClazz,Class<Target> targetClazz) throws InstantiationException, IllegalAccessException {
		if (source == null) {
			return targetClazz.newInstance();
		}
		Target targetBean = targetClazz.newInstance();
		
		BeanCopier beanCopier = getBeanCopier(sourceClazz, targetClazz);
		
		if (source != null) {
			beanCopier.copy(source, targetBean, null);
		}

		return targetBean;
	}
	
	private static <Source, Target> BeanCopier getBeanCopier(Class<Source> sourceClazz,Class<Target> targetClazz){
		Map<String, BeanCopier> targetBeanCopierMap = BEAN_COPIER_MAP.get(sourceClazz.getName());
		if(targetBeanCopierMap == null){
			targetBeanCopierMap = new HashMap<String, BeanCopier>();
		}
		BeanCopier beanCopier = targetBeanCopierMap.get(targetClazz.getName());
		if(beanCopier == null){
			beanCopier =  BeanCopier.create(sourceClazz, targetClazz, false);
		}
		
		return beanCopier;
	}
	
	public static <T> Map<String, Object> beanToMap(T bean) {  
	    Map<String, Object> map = Maps.newHashMap();  
	    if (bean != null) {  
	        BeanMap beanMap = BeanMap.create(bean);  
	        for (Object key : beanMap.keySet()) {  
	            map.put(key+"", beanMap.get(key));  
	        }             
	    }  
	    return map;  
	}  
	  
	/** 
	 * 将map装换为javabean对象 
	 * @param map 
	 * @return
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */  
	public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws InstantiationException, IllegalAccessException {
		T t = clazz.newInstance();
	    BeanMap beanMap = BeanMap.create(t);  
	    beanMap.putAll(map);  
	    return t;  
	}
	
	/** 
	 * 将List<T>转换为List<Map<String, Object>> 
	 * @param objList 
	 * @return
	 */  
	public static <T> List<Map<String, Object>> objectsToMaps(List<T> objList) {  
	    List<Map<String, Object>> list = Lists.newArrayList();  
	    if (objList != null && objList.size() > 0) {  
	        Map<String, Object> map = null;  
	        T bean = null;  
	        for (int i = 0,size = objList.size(); i < size; i++) {  
	            bean = objList.get(i);  
	            map = beanToMap(bean);  
	            list.add(map);  
	        }  
	    }  
	    return list;  
	}  
	  
	/** 
	 * 将List<Map<String,Object>>转换为List<T> 
	 * @param maps 
	 * @param clazz 
	 * @return 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 */  
	public static <T> List<T> mapsToObjects(List<Map<String, Object>> maps,Class<T> clazz) throws InstantiationException, IllegalAccessException {  
	    List<T> list = Lists.newArrayList();  
	    if (maps != null && maps.size() > 0) {  
	        Map<String, Object> map = null;  
	        T bean = null;  
	        for (int i = 0,size = maps.size(); i < size; i++) {  
	            map = maps.get(i);  
	            mapToBean(map, clazz);  
	            list.add(bean);  
	        }  
	    }  
	    return list;  
	}
	
}
